package me.firstapp.spi.bbs.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.bbs.JsonReply;
import me.firstapp.common.module.bbs.JsonReplyInManage;
import me.firstapp.spi.base.impl.BaseServiceImpl;
import me.firstapp.spi.bbs.ReplyService;

@Service
public class ReplyServiceImpl extends BaseServiceImpl implements ReplyService {
	@Override
	public SingleObject<Long> findTopicReplyCounts(Long topicId) {
		SingleObject<Long> result = new SingleObject<Long>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("topicId", topicId);
			JSONObject resultObject = api(parameters, "/reply/findTopicReplyCounts.do", HttpMethod.GET, "查询话题回复数量");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				Long counts = resultObject.getLong("object");
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(counts);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;

	}

	@Override
	public SingleObject<JsonReply> addReply(String content, Long topicId, Long fromMemberId, Long toMemberId) {
		SingleObject<JsonReply> result = new SingleObject<JsonReply>();
		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("content", content);

			Map<String, Object> parameters = new HashMap<>();
			parameters.put("topicId", topicId);
			parameters.put("fromMemberId", fromMemberId);
			parameters.put("toMemberId", toMemberId);

			JSONObject jsonObject = apiBody(parameters, "/reply/addReply.do", "新增回复", params);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JsonReply reply = JSONObject.toJavaObject(jsonObject.getJSONObject("object"), JsonReply.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(reply);
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public PageObject<JsonReply> findReplyPage(Integer pageNo, Integer pageSize, Long topicId, Long memberId) {
		PageObject<JsonReply> result = new PageObject<JsonReply>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("topicId", topicId);
			parameters.put("memberId", memberId);

			JSONObject resultObject = api(parameters, "/reply/findReplyPage.do", HttpMethod.GET, "分页查询话题回复");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JSONObject pageJsonObject = resultObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonReply> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonReply> replyList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonReply reply = JSONObject.toJavaObject(_jsonObject, JsonReply.class);
								replyList.add(reply);
							}
						}
					}
					jsonPage.setList(replyList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;

	}

	@Override
	public PageObject<JsonReplyInManage> findRepliesPageByParams(Long topicId, Integer pageNo, Integer pageSize,
			Map<String, Object> searchParams, String sortParam, String sortType) {
		PageObject<JsonReplyInManage> result = new PageObject<JsonReplyInManage>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("topicId", topicId);
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("sortParam", sortParam);
			parameters.put("sortType", sortType);
			JSONObject jsonObject = apiBody(parameters, "/reply/findRepliesPageByParams.do", "分页查询话题", searchParams);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JSONObject pageJsonObject = jsonObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonReplyInManage> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonReplyInManage> replyList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonReply _reply = JSONObject.toJavaObject(_jsonObject, JsonReply.class);
								JsonReplyInManage reply = new JsonReplyInManage(_reply);
								replyList.add(reply);
							}
						}
					}
					jsonPage.setList(replyList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Object> deleteReplies(String replyIds) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("replyIds", replyIds);

			JSONObject resultJson = api(parameters, "/reply/deleteReplies.do", HttpMethod.POST, "删除回复");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonReply> findReplyById(Long replyId) {
		SingleObject<JsonReply> result = new SingleObject<JsonReply>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("replyId", replyId);

			JSONObject resultJson = api(parameters, "/reply/findReplyById.do", HttpMethod.GET, "通过id获取回复");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JsonReply reply = JSONObject.toJavaObject(resultJson.getJSONObject("object"), JsonReply.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(reply);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Long> findTopicReplyCounts(Long topicId, Long apiClientId) {
		SingleObject<Long> result = new SingleObject<Long>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("topicId", topicId);
			parameters.put("apiClientId", apiClientId);
			
			JSONObject resultObject = api(parameters, "/batch/findTopicReplyCounts.do", HttpMethod.GET, "查询话题回复数量");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				Long counts = resultObject.getLong("object");
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(counts);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

}
